#ifndef __KERNEL_THREAD_H__
#define __KERNEL_THREAD_H__

#define THREAD_READY    0x01
#define THREAD_SUSPEND  0x02
#define THREAD_RUNNING  0x04
#define THREAD_DESTROY  0x08

#define THREAD_FLAG_STATIC  0x01
#define THREAD_FLAG_DYNAMIC 0x02

struct thread {
	struct object parent;
	struct list list;

	uint16_t flag;

	void *entry;
	void *parameter;
	void *stack_addr;
	base_t stack_size;
	void *exit;
	uint8_t *sp;

	ubase_t init_tick;
	ubase_t remaining_tick;

	uint8_t status;
	uint8_t priority;
	uint32_t mask_priority;

	struct timer timer;
};

/* 初始化线程 */
extern err_t thread_init(struct thread *thread,
			const char *name,
			void (*entry)(void *),
			void *parameter,
			base_t stack_size,
			uint8_t *stack,
			uint8_t priority);

/* 线程栈初始化 */
extern uint8_t *stack_init(void *entry,
				void *parameter,
				void *stack_addr,
				void *exit);

/* 上下文切换 */
extern void context_switch(ubase_t thread_from, ubase_t thread_to);

/* 上下文切换 */
extern void context_switch_to(ubase_t thread_to);

/* 线程创建 */
extern struct thread *thread_creat(const char *name,
			void (*entry)(void *),
			void *parameter,
			base_t stack_size,
			uint8_t priority);

/* 获取当前线程 */
extern struct thread *thread_self(void);

/* 查找一个线程 */
extern struct thread *thread_find(const char *name);

/* 将线程加入到就绪队列 */
extern err_t thread_resume(struct thread *thread);

/* 将线程挂起 */
extern err_t thread_suspend(struct thread *thread);

/* 启动线程 */
extern err_t thread_start(struct thread *thread);

/* 线程休眠函数 */
extern err_t thread_sleep(ubase_t tick);

/* 调度器初始化 */
extern void schedule_init(void);

/* 启动调度器 */
extern void schedule_start(void);

/* 将线程移除调度器(移除就绪队列) */
extern void schedule_remove(struct thread *thread);

/* 将一个线程加入到调度器中(加入就绪队列) */
extern void schedule_add(struct thread *thread);

/* 立即调度下一个线程 */
extern void schedule(void);

/* 放弃CPU使用权，并调度下一个线程 */
extern void yield(void);

/* 锁定任务调度器 */
extern void enter_critical(void);

/* 解锁任务调度器 */
extern void exit_critical(void);

#endif /* __KERNEL_THREAD_H__ */
